package com.ynr.voucher.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.ynr.voucher.dao.IDictionaryDao;
import com.ynr.voucher.model.ExpenseModel;
import com.ynr.voucher.model.InvoiceItemModel;
import com.ynr.voucher.model.InvoiceModel;
import com.ynr.voucher.model.SubjectModel;
import com.ynr.voucher.service.ISubjectService;

@Service
public class SubjectServiceImpl implements ISubjectService {

	private static final Logger logger = LoggerFactory.getLogger(SubjectServiceImpl.class);
	
	@Autowired
	private IDictionaryDao dicDao;
	
	@Override
	public List<SubjectModel> generateInvoiceSubjectList(InvoiceModel invoice, Map<String, Object> companyMap, InvoiceItemModel item, boolean isFa) {
		List<SubjectModel> retList = Lists.newArrayList();
		long companyId = (long)companyMap.get("id");
		int companyType = (int)companyMap.get("type");
		logger.debug("SubjectServiceImpl method generateInvoiceSubjectList, companyId : {}, companyType : {}", companyId, companyType);
		String itemContent = item.getName();
		//先查科目代码
		String subject = (isFa ? "1601" : invoiceDebitSubjectMatch(companyId, itemContent));
		//再查金额
		String debitCountStr = "";
		if(invoice.getFplx().equals("01") && companyType == 2) {
			debitCountStr = item.getPrice_sum();
		} else {
			debitCountStr = ""+(Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum()));
		}
		retList.add(new SubjectModel(subject, debitCountStr, ""));
		return retList;
	}
	
	@Override
	public List<SubjectModel> generateSalesSubjectList(InvoiceModel invoice, Map<String, Object> companyMap,
			InvoiceItemModel item) {
		List<SubjectModel> subjectList = new ArrayList<>();
		Double jshj = Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum());
		int exchangeDetail = (int)companyMap.get("exchange_detail");
		List<SubjectModel> debitSubject = null;
		SubjectModel creditSubject = new SubjectModel("", "5001", item.getPrice_sum());
		SubjectModel taxSubject = Double.valueOf(item.getTax_sum()) == 0.0 ? null : new SubjectModel("", "22210106", item.getTax_sum());
		if(exchangeDetail == 1) {
			long companyId = (long)companyMap.get("id");
			debitSubject = salesDebitSubjectMatch(companyId, invoice.getGfMc(), invoice.getBillDate(), jshj);
		}
		subjectList.addAll(debitSubject);
		if(taxSubject != null) {
			subjectList.add(taxSubject);
		}
		subjectList.add(creditSubject);
		return subjectList;
	}

	@Override
	public List<SubjectModel> generateExpenseSubjectList(Map<String, Object> companyMap, ExpenseModel expense) {
		List<SubjectModel> subjectList = new ArrayList<>();
		String abstract_str = expense.getExpense_type();
		String debitSubjectCode = invoiceDebitSubjectMatch((long)companyMap.get("id"), abstract_str);
		SubjectModel debitSubject = new SubjectModel(debitSubjectCode, expense.getAmount(), "");
		SubjectModel taxSubject = null;
		SubjectModel creditSubject = null;
		if(Double.valueOf(expense.getTax()) != 0.0 && (int)companyMap.get("type") == 2) {
			taxSubject = new SubjectModel("22210101", "", expense.getTax());
			creditSubject = new SubjectModel("224102", "", String.valueOf(Double.valueOf(expense.getAmount()) - Double.valueOf(expense.getTax())));
		} else {
			creditSubject = new SubjectModel("224102", "", expense.getAmount());
		}
		subjectList.add(debitSubject);
		if(taxSubject != null) {
			subjectList.add(taxSubject);
		}
		subjectList.add(creditSubject);
		return subjectList;
	}
	
	@Override
	public List<SubjectModel> generateBankSubjectList(Map<String, String> bsItem, Map<String, Object> companyMap, String billDate) {
		List<SubjectModel> subjectList = new ArrayList<>();
		long companyId = (long)companyMap.get("id");
		int exchangeDetail = (int)companyMap.get("exchange_detail");
		String pay_account_name = bsItem.get("pay_account_name");
		String abstract_ = bsItem.get("abstract_");
		String purpose = bsItem.get("purpose");
		String remark = bsItem.get("remark");
		if(bsItem.get("income").equals("")) {   //收入为空，支出不为空
			double cost = Double.valueOf(bsItem.get("cost"));
			if(pay_account_name.equals("上海市公积金管理中心") || abstract_.contains("公积金") || purpose.contains("公积金") || remark.contains("公积金")) {
				subjectList.add(new SubjectModel("224104", String.valueOf(cost/2), ""));
				subjectList.add(new SubjectModel("221105", String.valueOf(cost/2), ""));
				subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
				return subjectList;
			} else if(abstract_.contains("社保") || purpose.contains("社保") || remark.contains("社保")) {
				subjectList.add(new SubjectModel("224103", "", ""));
				subjectList.add(new SubjectModel("221104", "", ""));
				subjectList.add(new SubjectModel("1002", "", ""));
				return subjectList;
			} else if(abstract_.contains("个税") || purpose.contains("个税") || remark.contains("个税")) {
				subjectList.add(new SubjectModel("224101", String.valueOf(cost), ""));
				subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
				return subjectList;
			} else if(pay_account_name.equals("其他待报解预算收入") || (abstract_.contains("税")&&!abstract_.contains("个税")) || (purpose.contains("税")&&!purpose.contains("个税")) || (remark.contains("税")&&!remark.contains("个税"))) {
				subjectList.add(new SubjectModel("税", "", ""));
				subjectList.add(new SubjectModel("税", "", ""));
				return subjectList;
			} else if(pay_account_name.equals("")) {
				//TODO
				List<Map<String, Object>> companyBankDicList = dicDao.getCompanyBankDic(companyId, 0);
				List<Map<String, Object>> commonBankDicList = dicDao.getCommonBankDic(0);
				for(Map<String, Object> companyBankDic : companyBankDicList) {
					String content = (String)companyBankDic.get("content");
					if(abstract_.contains(content) || purpose.contains(content) || remark.contains(content)) {
						subjectList.add(new SubjectModel((String)companyBankDic.get("subject"), String.valueOf(cost), ""));
						subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
						return subjectList;
					}
				}
				for(Map<String, Object> commonBankDic : commonBankDicList) {
					String content = (String)commonBankDic.get("content");
					if(abstract_.contains(content) || purpose.contains(content) || remark.contains(content)) {
						subjectList.add(new SubjectModel((String)commonBankDic.get("subject"), String.valueOf("cost"), ""));
						subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
						return subjectList;
					}
				}
				subjectList.add(new SubjectModel("", String.valueOf(cost), ""));
				subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
			} else {
				if(exchangeDetail == 0) {   //往来不分明细
					subjectList.add(new SubjectModel("2202", String.valueOf(cost), ""));
					subjectList.add(new SubjectModel("1002", "", String.valueOf(cost)));
					return subjectList;
				} else {   //往来分明细
					subjectList.addAll(bankCostSubjectMatch(companyId, bsItem.get("pay_account_name"), billDate, bsItem.get("cost")));
					subjectList.add(new SubjectModel("1002", "", bsItem.get("cost")));
					return subjectList;
				}
			}
		} else {
			String income_ = bsItem.get("income");
			if(pay_account_name.equals("")) {
				if(pay_account_name.equals("结息") || (abstract_.contains("结息")&&!abstract_.contains("结息")) || (purpose.contains("结息")&&!purpose.contains("结息")) || (remark.contains("税")&&!remark.contains("个税"))) {
					subjectList.add(new SubjectModel("1002", income_, ""));
					subjectList.add(new SubjectModel("560301", ""+(0-Double.valueOf(income_)), ""));
				} else {
					//TODO
					List<Map<String, Object>> companyBankDicList = dicDao.getCompanyBankDic(companyId, 1);
					List<Map<String, Object>> commonBankDicList = dicDao.getCommonBankDic(1);
					for(Map<String, Object> companyBankDic : companyBankDicList) {
						String content = (String)companyBankDic.get("content");
						if(abstract_.contains(content) || purpose.contains(content) || remark.contains(content)) {
							subjectList.add(new SubjectModel("1002", income_, ""));
							subjectList.add(new SubjectModel((String)companyBankDic.get("subject"), "", income_));
							return subjectList;
						}
					}
					for(Map<String, Object> commonBankDic : commonBankDicList) {
						String content = (String)commonBankDic.get("content");
						if(abstract_.contains(content) || purpose.contains(content) || remark.contains(content)) {
							subjectList.add(new SubjectModel("1002", income_, ""));
							subjectList.add(new SubjectModel((String)commonBankDic.get("subject"), "", income_));
							return subjectList;
						}
					}
					subjectList.add(new SubjectModel("1002", income_, ""));
					subjectList.add(new SubjectModel("", "", income_));
				}
			} else if(exchangeDetail == 0){
				subjectList.add(new SubjectModel("1002", income_, ""));
				subjectList.add(new SubjectModel("1122", "", income_));
				return subjectList;
			} else if(exchangeDetail == 1) {
				subjectList.add(new SubjectModel("1002", income_, ""));
				subjectList.addAll(bankIncomeSubjectMatch(companyId, bsItem.get("pay_account_name"), billDate, income_));
				return subjectList;
			}
		}
		return subjectList;
	}
	
	@Override
	public List<SubjectModel> generateSalarySubjectList(Map<String, Object> salaryItem) {
		
		double real_wage = "".equals(salaryItem.get("real_wage"))?0:Double.valueOf(""+salaryItem.get("real_wage"));
		double tax = "".equals(salaryItem.get("tax"))?0:Double.valueOf(""+salaryItem.get("tax"));
		double house_provident_company = "".equals(salaryItem.get("house_provident_company"))?0:Double.valueOf(""+salaryItem.get("house_provident_company"));
		double birth_company = "".equals(salaryItem.get("birth_company"))?0:Double.valueOf(""+salaryItem.get("birth_company"));
		double injury_company = "".equals(salaryItem.get("injury_company"))?0:Double.valueOf(""+salaryItem.get("injury_company"));
		double medical_company = "".equals(salaryItem.get("medical_company"))?0:Double.valueOf(""+salaryItem.get("medical_company"));
		double unemployment_company = "".equals(salaryItem.get("unemployment_company"))?0:Double.valueOf(""+salaryItem.get("unemployment_company"));
		double endowment_company = "".equals(salaryItem.get("endowment_company"))?0:Double.valueOf(""+salaryItem.get("endowment_company"));
		double house_provident_person = "".equals(salaryItem.get("house_provident_person"))?0:Double.valueOf(""+salaryItem.get("house_provident_person"));
		double medical_person = "".equals(salaryItem.get("medical_person"))?0:Double.valueOf(""+salaryItem.get("medical_person"));
		double unemployment_person = "".equals(salaryItem.get("unemployment_person"))?0:Double.valueOf(""+salaryItem.get("unemployment_person"));
		double endowment_person = "".equals(salaryItem.get("endowment_person"))?0:Double.valueOf(""+salaryItem.get("endowment_person"));
		
		double sum_insurance_company = birth_company+injury_company+medical_company+unemployment_company+endowment_company;
		double sum_house_provident_company = house_provident_company;
		double sum_insurance_person = medical_person + unemployment_person + endowment_person;
		double sum_house_provident_person = house_provident_person;
		double sum_tax = tax;
		double sum_real_wage = real_wage;
		
		double debit = sum_insurance_company + sum_house_provident_company + sum_insurance_person + sum_house_provident_person + sum_tax + sum_real_wage;
		String department = ""+salaryItem.get("department");
		List<SubjectModel> subjectList = new ArrayList<>();
		
		SubjectModel debitSubject = new SubjectModel(department.contains("研发")?"560203":(department.contains("销售")?"560107":"560209"), String.valueOf(debit), "");
		SubjectModel realWageSubject = sum_real_wage==0?null:new SubjectModel("221101", "", String.valueOf(sum_real_wage));
		SubjectModel insuranceCompanySubject = sum_insurance_company==0?null:new SubjectModel("221104", "", String.valueOf(sum_insurance_company));
		SubjectModel houseProvidentCompanySubject = house_provident_company==0?null:new SubjectModel("221105", "", String.valueOf(house_provident_company));
		SubjectModel taxSubject = sum_tax==0?null:new SubjectModel("224101", "", String.valueOf(sum_tax));
		SubjectModel insurancePersonSubject = sum_insurance_person==0?null:new SubjectModel("224103", "", String.valueOf(sum_insurance_person));
		SubjectModel houseProvidentPersonSubject = sum_house_provident_person==0?null:new SubjectModel("224104", "", String.valueOf(sum_house_provident_person));
		
		subjectList.add(debitSubject);
		if(realWageSubject != null) {
			subjectList.add(realWageSubject);
		}
		if(insuranceCompanySubject != null) {
			subjectList.add(insuranceCompanySubject);
		}
		if(houseProvidentCompanySubject != null) {
			subjectList.add(houseProvidentCompanySubject);
		}
		if(taxSubject != null) {
			subjectList.add(taxSubject);
		}
		if(insurancePersonSubject != null) {
			subjectList.add(insurancePersonSubject);
		}
		if(houseProvidentPersonSubject != null) {
			subjectList.add(houseProvidentPersonSubject);
		}
		return subjectList;
	}

	//invoice贷方科目，先查科目属性表有没有1123开头预付的这家公司，如果有，查余额，余额大则进1123xx，如果余额小则进1123xx一部分，2202xx一部分；如果没有则去查2202应付，有直接进2202xx，如无创建2202xx
	@Override
	public List<SubjectModel> invoiceCreditSubjectMatch(long companyId, String supplyName, String billDate, double jshj) {
		String subject;
		List<SubjectModel> retList = new ArrayList<>();
		//科目属性表中查1123预付
		Map<String, Object> subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1123");
		if(subjectAttribute == null || subjectAttribute.isEmpty()) {   //查不到1123,再查2202
			subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2202");
			if(subjectAttribute == null || subjectAttribute.isEmpty()) {
				//获取应付账款1123xx的最后一个
				String lastCode = dicDao.getLastPayableCode(companyId, "2202");
				if(lastCode.length() == 4) {
					subject = "220201";
				} else {
					subject = ("" + (Integer.valueOf(lastCode)+1));
				}
				dicDao.addSubjectAttribute("FZ", 2, 2, subject, supplyName, 1, 2, companyId);//auxiliary_account_type,type,level,subject_code,subject_name,is_last,balance_direction,company_id
				retList.add(new SubjectModel(subject, "", ""+jshj));
				return retList;
			} else {
				retList.add(new SubjectModel("" + subjectAttribute.get("subject_code"), "", ""+jshj));
				return retList;
			}
		} else {  //查到1123
			Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1123");
			double remain = Double.valueOf(""+trialBalance.get("initial_debit"));
			if(remain >= jshj) {
				retList.add(new SubjectModel(""+trialBalance.get("subject_code"), "", ""+jshj));
				return retList;
			} else {
				if(remain != 0) {
					retList.add(new SubjectModel(""+trialBalance.get("subject_code"), "", ""+remain));
				}
				subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2202");
				if(subjectAttribute == null || subjectAttribute.isEmpty()) {
					//获取应付账款2202xx的最后一个
					String lastCode = dicDao.getLastPayableCode(companyId, "2202");
					if(lastCode.length() == 4) {
						subject = "220201";
					} else {
						subject = ("" + (Integer.valueOf(lastCode)+1));
					}
					dicDao.addSubjectAttribute("FZ", 2, 2, subject, supplyName, 1, 2, companyId);
					retList.add(new SubjectModel(subject, "", ""+(jshj-remain)));
				} else {
					trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "2202");
					retList.add(new SubjectModel("" + trialBalance.get("subject_code"), "", ""+(jshj-remain)));
				}
				return retList;
			}
		}
	}

	//invoice借方科目
	private String invoiceDebitSubjectMatch(long companyId, String itemContent) {
		String subject = "5602";
		List<Map<String, Object>> companySubjectDicList = dicDao.getCompanySubjectDic(companyId);
		List<Map<String, Object>> commonSubjectDicList = dicDao.getCommonSubjectDic();
		for(Map<String, Object> companySubject : companySubjectDicList) {
			String subjectKeyword = (String)companySubject.get("content");
			if(itemContent.contains(subjectKeyword)) {
				return (String)companySubject.get("subject");
			}
		}
		for(Map<String, Object> commonSubject : commonSubjectDicList) {
			String subjectKeyword = (String)commonSubject.get("content");
			if(itemContent.contains(subjectKeyword)) {
				return (String)commonSubject.get("subject");
			}
		}
		return subject;
	}

	//sales借方科目，先查科目属性表有没有2203开头预收的这家公司，如果有，查余额，余额大则进2203xx，如果余额小则进2203xx一部分，1122xx一部分；如果没有则去查1122应收，有直接进1122xx，如无创建1122xx
	private List<SubjectModel> salesDebitSubjectMatch(long companyId, String supplyName, String billDate, double jshj) {
		String subject;
		List<SubjectModel> retList = new ArrayList<>();
		Map<String, Object> subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2203");
		if(subjectAttribute == null || subjectAttribute.isEmpty()) {   //查不到2203,没预收
			subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1122");
			//trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1122"); //查1122应收账款
			if(subjectAttribute == null || subjectAttribute.isEmpty()) {
				//获取应收1122账款xx的最后一个
				String lastCode = dicDao.getLastPayableCode(companyId, "1122");
				if(lastCode.length() == 4) {
					subject = "112201";
				} else {
					subject = ("" + (Integer.valueOf(lastCode)+1));
				}
				dicDao.addSubjectAttribute("ZC", 1, 1, subject, supplyName, 1, 1, companyId);
				retList.add(new SubjectModel(subject, ""+jshj, ""));
				return retList;
			} else {
				Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1122");
				retList.add(new SubjectModel("" + trialBalance.get("subject_code"), ""+jshj, ""));
				return retList;
			}
		} else {  //查到2203
			Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "2203");
			double remain = Double.valueOf(""+trialBalance.get("initial_credit"));
			if(remain >= jshj) {
				retList.add(new SubjectModel(""+trialBalance.get("subject_code"), ""+jshj, ""));
				return retList;
			} else {
				if(remain != 0) {
					retList.add(new SubjectModel(""+trialBalance.get("subject_code"), ""+remain, ""));
				}
				subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1122");
				if(subjectAttribute == null || subjectAttribute.isEmpty()) {
					//获取应付账款1122xx的最后一个
					String lastCode = dicDao.getLastPayableCode(companyId, "1122");
					if(lastCode.length() == 4) {
						subject = "112201";
					} else {
						subject = ("" + (Integer.valueOf(lastCode)+1));
					}
					dicDao.addSubjectAttribute("ZC", 1, 1, subject, supplyName, 1, 1, companyId);
					retList.add(new SubjectModel(subject, ""+(jshj-remain), ""));
				} else {
					trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1122");
					retList.add(new SubjectModel("" + trialBalance.get("subject_code"), ""+(jshj-remain), ""));
				}
				return retList;
			}
		}
	}
	
	//银行对账单支出科目
	private List<SubjectModel> bankCostSubjectMatch(long companyId, String supplyName, String billDate, String cost) {
		String subject;
		List<SubjectModel> retList = new ArrayList<>();
		//不写科目余额表，会导致科余找不到，但是属性表里有，然后重复增加科目属性表
		Map<String, Object> subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2202");
		if(subjectAttribute == null || subjectAttribute.isEmpty()) {   //查科目属性表2202结尾
			subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1123");
			//trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1123"); 
			if(subjectAttribute == null || subjectAttribute.isEmpty()) {
				String lastCode = dicDao.getLastPayableCode(companyId, "1123");
				if(lastCode.length() == 4) {
					subject = "112301";
				} else {
					subject = ("" + (Integer.valueOf(lastCode)+1));
				}
				dicDao.addSubjectAttribute("ZC", 1, 2, subject, supplyName, 1, 1, companyId);
				retList.add(new SubjectModel(subject, cost, ""));
				return retList;
			} else {
				Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1123"); 
				retList.add(new SubjectModel("" + trialBalance.get("subject_code"), cost, ""));
				return retList;
			}
		} else {
			Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "2202");
			double remain = Double.valueOf(""+trialBalance.get("initial_credit"));
			if(remain >= Double.valueOf(cost)) {
				retList.add(new SubjectModel(""+trialBalance.get("subject_code"), cost, ""));
				return retList;
			} else {
				if(remain != 0) {
					retList.add(new SubjectModel(""+trialBalance.get("subject_code"), ""+remain, ""));
				}
				subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1123");
				if(subjectAttribute == null || subjectAttribute.isEmpty()) {
					String lastCode = dicDao.getLastPayableCode(companyId, "1123");
					if(lastCode.length() == 4) {
						subject = "112301";
					} else {
						subject = ("" + (Integer.valueOf(lastCode)+1));
					}
					dicDao.addSubjectAttribute("ZC", 1, 2, subject, supplyName, 1, 1, companyId);
					retList.add(new SubjectModel(subject, ""+(Double.valueOf(cost)-remain), ""));
				} else {
					trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1123");
					retList.add(new SubjectModel("" + trialBalance.get("subject_code"), ""+(Double.valueOf(cost)-remain), ""));
				}
				return retList;
			}
		}
	}
	
	
	//银行对账单收入科目
	private List<SubjectModel> bankIncomeSubjectMatch(long companyId, String supplyName, String billDate, String income) {
		String subject;
		List<SubjectModel> retList = new ArrayList<>();
		Map<String, Object> subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "1122");
		//Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1122");
		if(subjectAttribute == null || subjectAttribute.isEmpty()) {   
			subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2203");
			if(subjectAttribute == null || subjectAttribute.isEmpty()) {
				String lastCode = dicDao.getLastPayableCode(companyId, "2203");
				if(lastCode.length() == 4) {
					subject = "220301";
				} else {
					subject = ("" + (Integer.valueOf(lastCode)+1));
				}
				dicDao.addSubjectAttribute("FZ", 2, 2, subject, supplyName, 1, 2, companyId);
				retList.add(new SubjectModel(subject, "", income));
				return retList;
			} else {
				Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "2203");
				retList.add(new SubjectModel("" + trialBalance.get("subject_code"), "", income));
				return retList;
			}
		} else {
			Map<String, Object> trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "1122");
			double remain = Double.valueOf(""+trialBalance.get("initial_credit"));
			if(remain >= Double.valueOf(income)) {
				retList.add(new SubjectModel(""+trialBalance.get("subject_code"), "", income));
				return retList;
			} else {
				if(remain != 0) {
					retList.add(new SubjectModel(""+trialBalance.get("subject_code"), "", ""+remain));
				}
				subjectAttribute = dicDao.getSubjectBySupplyName(companyId, supplyName, "2203");
				if(subjectAttribute == null || subjectAttribute.isEmpty()) {
					String lastCode = dicDao.getLastPayableCode(companyId, "2203");
					if(lastCode.length() == 4) {
						subject = "220301";
					} else {
						subject = ("" + (Integer.valueOf(lastCode)+1));
					}
					dicDao.addSubjectAttribute("FZ", 2, 2, subject, supplyName, 1, 2, companyId);
					retList.add(new SubjectModel(subject, "", income));
				} else {
					trialBalance = dicDao.getTrialBalance(companyId, supplyName, billDate, "2203");
					retList.add(new SubjectModel("" + trialBalance.get("subject_code"), "", ""+(Double.valueOf(income)-remain)));
				}
				return retList;
			}
		}
	}

	@Override
	public List<SubjectModel> generateFaSubjectList(String monthlyDep) {
		List<SubjectModel> retList = Lists.newArrayList();
		SubjectModel debitSubjectModel = new SubjectModel("560212", monthlyDep, "");
		SubjectModel creditSubjectModel = new SubjectModel("1602", "", monthlyDep);
		retList.add(debitSubjectModel);
		retList.add(creditSubjectModel);
		return retList;
	}

	
}
